package cn.org.mrliu.ssm.qq;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import cn.org.mrliu.ssm.qq.config.QQConfigUtil;

public class QQMessageQueue {
	// 队列大小
	public static final int QUEUE_MAX_SIZE = QQConfigUtil.getConfig().getQueueMaxSize();
	// 阻塞队列
	private BlockingQueue<QQMessage> blockingQueue = new LinkedBlockingQueue<QQMessage>(QUEUE_MAX_SIZE);
	private static QQMessageQueue qqMessageQueue = new QQMessageQueue();

	private QQMessageQueue() {

		}

	public static QQMessageQueue getInstance() {
		return qqMessageQueue;
	}

	/**
	 * 消息入队
	 * 
	 * @param qqMessage
	 * @return
	 */
	public boolean push(QQMessage qqMessage) {
		return this.blockingQueue.offer(qqMessage);
	}

	/**
	 * 消息出队
	 * 
	 * @return
	 */
	public QQMessage poll() {
		QQMessage qqMessage = null;
		try {
			qqMessage = this.blockingQueue.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return qqMessage;
	}

	/**
	 * 获取队列大小
	 * 
	 * @return
	 */
	public int size() {
		return this.blockingQueue.size();
	}
}
